package com.szqhyxj.youxiangju.biz.receivables;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.ReceiptDetailModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2019/1/8.
 */

public class ReceivablesPresenter implements ReceivablesContract.ReceivablesPresenter {
    private ReceivablesContract.ReceivablesView view;

    private ReceivablesModel.Receivables receivables;
    private List<Disposable> disposables = new ArrayList<>();

    public ReceivablesPresenter(ReceivablesContract.ReceivablesView view) {
        this.view = view;
        this.receivables = new ReceivablesModel.Receivables();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init() {

    }

    @Override
    public void setAmount(String amount, String balance) {
        if (amount == null || amount.equals("")) {
            view.showTextSize();
            view.showEnClickable();
            view.setTip1("手续费：单笔2元");
            return;
        } else if (Double.parseDouble(amount) == 0) {
            view.showEnClickable();
        } else if (Double.parseDouble(amount) > 0 && Double.parseDouble(amount) < 100) {
            view.setTip("单笔最低100元");
            view.showEnClickable();
        } else if (Double.parseDouble(amount) > 10000) {
            view.setTip("单笔最高1万元");
            view.showEnClickable();
        } else if (Double.parseDouble(amount) > Double.parseDouble(balance)) {
            view.setTip("不能大于账户余额");
            view.showEnClickable();
        } else {
            receivables.setWithdrawAmt(amount);
            checkRegisterEnable();
            view.setTip1("手续费：单笔2元");
            checkRegisterEnable();
        }

    }

    @Override
    public void getCard() {
        ReceiptDetailModel.getCardNo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<String>>() {
                    @Override
                    public void accept(BaseResult<String> stringBaseResult) throws Exception {
                        if (stringBaseResult.businessLogicSuccess()) {
                            view.showsavaCard(stringBaseResult.getData() + "");
                        } else {
                            view.showToast(stringBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void commit() {
        view.showLoading();
        ReceivablesModel.getReceivables(this.receivables)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<ReceivablesModel.ReceivablesBean>>() {
                    @Override
                    public void accept(BaseResult<ReceivablesModel.ReceivablesBean> receivablesBeanBaseResult) throws Exception {
                        view.dismissLoading();
                        if (receivablesBeanBaseResult.businessLogicSuccess()) {
                            String id = receivablesBeanBaseResult.getData().getWithdrawId();
                            //view.showToast("成功"+id);
                            view.showReceivablesDetail(id);
                            view.addBean(receivables);
                        } else {
                            view.showToast(receivablesBeanBaseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage() + "");
                    }
                });
    }

    @Override
    public void getUserInfo() {
        UserInfoModel
                .loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new BaseConsumer<UserInfo>() {
                            @Override
                            protected void error(String code, String msg) {
                                //view.shownetpopview();
                            }

                            @Override
                            protected void successAccept(UserInfo userInfo) {
                                UserInfoManger.INSTANCE.init(userInfo);


                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //    view.shownetpopview();
                            }
                        });
    }

    @Override
    public void getSupportBank() {
        ReceivablesModel.getSupportBank()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<List<ReceivablesModel.Response>>>() {
                    @Override
                    public void accept(BaseResult<List<ReceivablesModel.Response>> listBaseResult) throws Exception {
                        if (listBaseResult.businessLogicSuccess()) {
                            List<ReceivablesModel.Response> list = listBaseResult.getData();
                            if (listBaseResult.getData().size() == 0) {
                                view.showToast("发卡行列表为空");
                            } else {
                                view.showSupportBankList(list);
                            }
                        } else {
                            view.showToast(listBaseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });


    }

    private void checkRegisterEnable() {
        if (receivables.valid()) {
            view.showClickable();

        } else {
            view.showEnClickable();

        }
    }


    @Override
    public void setBtName(String btname) {
        receivables.setBtName(btname);
        checkRegisterEnable();
    }

    @Override
    public void setBtCode(String btCode) {
        receivables.setBtCode(btCode);
        checkRegisterEnable();
    }

    @Override
    public void setBankCard(String bankCard) {
        receivables.setBankCardNo(bankCard);
        checkRegisterEnable();
    }

    @Override
    public void setCardPhone(String cardPhone) {
        receivables.setPhone(cardPhone);
        checkRegisterEnable();
    }

    @Override
    public void setVerificationCode(String verificationCode) {
        receivables.setCode(verificationCode);
        checkRegisterEnable();
    }

    private static final int MINUTE = 60;

    @Override
    public void acquireVerificationCode() {
        view.showLoading();
        String phone = this.receivables.getPhone();
        disposables.add(ReceivablesModel.realNameSms(phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Consumer<BaseResult>() {
                            @Override
                            public void accept(BaseResult baseResult) {
                                view.dismissLoading();
                                if (baseResult.businessLogicSuccess()) {
                                    view.showAcquiredVerificationCodeSuccess();
                                    view.showAcquiringVerificationCode(MINUTE);
                                    startCountDown();
                                } else {
                                    view.showToast(baseResult.getMessage());
                                }
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                view.dismissLoading();
                                view.showToast(throwable.getMessage());
                            }
                        }
                )
        );
    }

    private void startCountDown() {
        disposables.add(Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        if (aLong == MINUTE - 1) {
                            view.showAcquiredVerificationCode();
                        } else {
                            view.showAcquiringVerificationCode((int) (MINUTE - aLong - 1));
                        }
                    }
                }));
    }


}
